//
//  FriendRequestCell.m
//  TheProjectName
//
//  Created by 李劲成 on 16/5/23.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "FriendRequestCell.h"

@interface FriendRequestCell ()
/** 头像 */
@property (nonatomic, retain) UIImageView *m_photo;
/** 申请人姓名控件 */
@property (nonatomic, retain) UILabel *name;
/** 申请人消息控件 */
@property (nonatomic, retain) UILabel *message;
/** 同意按钮 */
@property (nonatomic, retain) UIButton *accept;
/** 拒绝按钮 */
@property (nonatomic, retain) UIButton *refuse;

@end

@implementation FriendRequestCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

#pragma mark - 重写初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"clear"]];
        [self createSubViews];
    }
    return self;
}

#pragma mark - 创建子视图
- (void)createSubViews{
    
    self.m_photo = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 60, 60)];
    // 切圆
    UIImage *image = [UIImage imageNamed:@"1111.jpg"];
    UIGraphicsBeginImageContextWithOptions(_m_photo.bounds.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:_m_photo.bounds cornerRadius:30]addClip];
    [image drawInRect:_m_photo.bounds];
    _m_photo.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.contentView addSubview:_m_photo];
    
    // 姓名 Label
    self.name = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, SCREEN_WIDTH - 240, 25)];
    [self.contentView addSubview:_name];
    
    // 消息 Label
    self.message = [[UILabel alloc]initWithFrame:CGRectMake(90, 45, SCREEN_WIDTH - 240, 25)];
    _message.font = [UIFont systemFontOfSize:10];
    _message.textColor = [UIColor grayColor];
    [self.contentView addSubview:_message];
    
    // 同意
    self.accept = [UIButton buttonWithType:UIButtonTypeSystem];
    [_accept addTarget:self action:@selector(handleAccept:) forControlEvents:UIControlEventTouchUpInside];
    _accept.frame = CGRectMake(SCREEN_WIDTH - 140, 20, 60, 40);
    [_accept setTitle:@"同意" forState:UIControlStateNormal];
    [_accept setBackgroundColor:[UIColor greenColor]];
    [_accept setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.contentView addSubview:_accept];
    
    // 拒绝
    self.refuse = [UIButton buttonWithType:UIButtonTypeSystem];
    [_refuse addTarget:self action:@selector(handleRefuse:) forControlEvents:UIControlEventTouchUpInside];
    _refuse.frame = CGRectMake(SCREEN_WIDTH - 70, 20, 60, 40);
    [_refuse setTitle:@"拒绝" forState:UIControlStateNormal];
    [_refuse setBackgroundColor:[UIColor  redColor]];
    [_refuse setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.contentView addSubview:_refuse];
}


#pragma mark - 点击同意事件
- (void)handleAccept:(UIButton *)button{
    EMError *error = [[EMClient sharedClient].contactManager acceptInvitationForUsername:_name.text];
    if (!error) {
        self.handleClick();
    }
}
#pragma mark - 点击拒绝事件
- (void)handleRefuse:(FriendRequestCell *)cell{
    EMError *error = [[EMClient sharedClient].contactManager declineInvitationForUsername:_name.text];
    if (!error) {
         self.handleClick();
    }

}
- (void)setName:(NSString *)name andMessage:(NSString *)message{
    _name.text = name;
    _message.text = [NSString stringWithFormat:@"%@请求添加好友", _name.text];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
